from typing import *


class ListNode:

    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def print_list_node(head):
    nums = []
    while head:
        nums.append(head.val)
        head = head.next
    print(nums)
class Solution:

    def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:

        def reverse(head):
            p = None
            while head:
                nxt = head.next
                head.next = p
                p = head
                head = nxt
            return p

        def remove(head):
            t, p = head, head.next
            while p:
                if p.val >= t.val:
                    t.next = p
                    t = t.next
                else:
                    t.next = None
                p = p.next
            return head
        return reverse(remove(reverse(head)))
    
nums = [1,1,1,1]

head = ListNode(val = nums[0])
cur = head
for i in range(1,len(nums)):
    node = ListNode(val = nums[i])
    cur.next = node
    cur = cur.next
s = Solution()
print_list_node(s.removeNodes(head))